//
//  FileManageTool.m
//  iOSBaseFramework
//
//  Created by user on 2020/6/16.
//  Copyright © 2020 com.taylor. All rights reserved.
//

#import "TRFileManageTool.h"
#import <UIKit/UIKit.h>

@interface TRFileManageTool()

@property (nonatomic, strong) NSFileManager *fileManager;

@end

@implementation TRFileManageTool

- (NSFileManager *)fileManager {
    
    if (!_fileManager) {
        _fileManager = [NSFileManager defaultManager];
    }
    return _fileManager;
}

+ (instancetype) shareManager {
    static TRFileManageTool *manager = nil;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        manager = [[TRFileManageTool alloc] init];
    });
    return manager;
}

- (NSString *)createFilePathWithFilename:(NSString *)fileName folder:(NSString *)folder type:(TRVDFileType)type
{
    NSString *path, *folderPath, *filePath;
    switch (type) {
        case TRVDFileTypeDocument:
            path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
            break;
        case TRVDFileTypeCaches:
            path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;
            break;
        case TRVDFileTypeTmp:
            path = NSTemporaryDirectory();
            break;
        default:
            break;
    }
    
    if (folder.length > 0)
    {
        folderPath = [path stringByAppendingPathComponent:folder];
        BOOL isDir = YES;
        if(![self.fileManager fileExistsAtPath:folderPath isDirectory:&isDir]) {
            [self.fileManager createDirectoryAtPath:folderPath withIntermediateDirectories:YES attributes:nil error:nil];
        }
    } else {
        folderPath = path;
    }
    
    filePath = [folderPath stringByAppendingPathComponent:fileName];
    NSLog(@"file path:%@", filePath);
    return filePath;
}

- (BOOL)saveToLocalFolderWithFile:(id)file withFilePath:(NSString *)filePath
{
    if ([self.fileManager fileExistsAtPath:filePath]) {
        return NO;
    }
    
    NSData *fileData;
    if ([file isKindOfClass:[NSData class]]) {
        fileData = file;
    }
    else if ([file isKindOfClass:[UIImage class]]) {
        fileData = UIImageJPEGRepresentation(file, 1.0);
    }
    else {
        return NO;
    }
    return [fileData writeToFile:filePath atomically:YES];
}


- (BOOL)writeToFileWithContent:(id)content path:(NSString *)path{
    if ([content isKindOfClass:[NSString class]]) {
        return [content writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
    }
    else
    {
        return [content writeToFile:path atomically:YES];
    }
}


- (NSString *)readFileContentWithFilePath:(NSString *)path
{
    return [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
}


- (BOOL)deleteFileWithFilePath:(NSString *)path {
    return [self.fileManager removeItemAtPath:path error:nil];
}

- (unsigned long long) fileSizeAtPath:(NSString *)path{
    if ([self.fileManager fileExistsAtPath:path]) {
        unsigned long long fileSize = [[self.fileManager attributesOfItemAtPath:path error:nil] fileSize];
        return fileSize;
    } else {
        return 0;
    }
}


- (unsigned long long)folderSizeAtPath:(NSString *)path {
    if ([self.fileManager fileExistsAtPath:path]) {
        NSEnumerator *childFileEnumerator = [[self.fileManager subpathsAtPath:path] objectEnumerator];
        unsigned long long folderSize = 0;
        NSString *fileName = @"";
        while ((fileName = [childFileEnumerator nextObject]) != nil) {
            NSString *fileAbsolutePath = [path stringByAppendingPathComponent:fileName];
            folderSize += [self fileSizeAtPath:fileAbsolutePath];
        }
        return folderSize;
    } else {
        return 0;
    }
}


@end
